package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.MchntAuthCtrlMapper;
import cc.rengu.igas.bomp.common.dao.impl.MchntAuthCtrlMapperImpl;
import cc.rengu.igas.bomp.common.entity.MchntAuthCtrl;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.MchntAuthCtrlInfoBean;
import cc.rengu.igas.bomp.facade.request.MchntTxnAuthQueryRequest;
import cc.rengu.igas.bomp.facade.response.MchntTxnAuthQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class MchntTxnAuthQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntTxnAuthQueryRequest mchntTxnAuthQueryRequest = new MchntTxnAuthQueryRequest();
        ConvertUtil.convertOutput(mchntTxnAuthQueryRequest);
        return mchntTxnAuthQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        MchntTxnAuthQueryRequest mchntTxnAuthQueryRequest = (MchntTxnAuthQueryRequest) request;
        MchntTxnAuthQueryResponse mchntTxnAuthQueryResponse = new MchntTxnAuthQueryResponse();
        BizResponse<MchntTxnAuthQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);

        String instId = mchntTxnAuthQueryRequest.getHeader().getInstId();
        String mchntNo = mchntTxnAuthQueryRequest.getMchntNo();
        String ctrlType = mchntTxnAuthQueryRequest.getCtrlType();
        String termNo = mchntTxnAuthQueryRequest.getTermNo();
        String txnNum = mchntTxnAuthQueryRequest.getTxnNum();
        String bizType = mchntTxnAuthQueryRequest.getBizType();

        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(ctrlType) || StringUtil.isEmptyOrNull(mchntNo) || StringUtil.isEmptyOrNull(termNo)) {
            rglog.error("请求参数不合法,instId:<{}>,crtlType:<{}>,mchntNo:<{}>,termNo:<{}>", instId, ctrlType, mchntNo, termNo);
            throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
        }

        MchntAuthCtrlMapper mchntAuthCtrlMapper = new MchntAuthCtrlMapperImpl();
        List<MchntAuthCtrl> mchntAuthCtrlList = mchntAuthCtrlMapper.selectMchntAuthCtrlInfoByCtrlInst(instId, ctrlType, mchntNo, termNo);
        if (null == mchntAuthCtrlList || mchntAuthCtrlList.isEmpty()) {
            rglog.error("获取商户权限信息失败,instId:<{}>,crtlType:<{}>,mchntNo:<{}>,termNo:<{}>", instId, ctrlType, mchntNo, termNo);
            bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
            bizResponse.setResult(mchntTxnAuthQueryResponse);
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
        if (!StringUtil.isEmptyOrNull(txnNum)) {
            mchntAuthCtrlList = mchntAuthCtrlList.stream().filter(item -> item.getTxnNum().equals(txnNum)).collect(Collectors.toList());
        }
        if (!StringUtil.isEmptyOrNull(bizType)) {
            mchntAuthCtrlList = mchntAuthCtrlList.stream().filter(item -> item.getBizType().equals(bizType)).collect(Collectors.toList());
        }
        List<MchntAuthCtrlInfoBean> mchntAuthCtrlInfoBeanList = new ArrayList<>();
        for (MchntAuthCtrl mchntAuthCtrl : mchntAuthCtrlList) {
            MchntAuthCtrlInfoBean mchntAuthCtrlInfoBean = new MchntAuthCtrlInfoBean();
            BeanUtil.beanCopy(mchntAuthCtrl, mchntAuthCtrlInfoBean);
            mchntAuthCtrlInfoBean.setBaseProdCode(mchntAuthCtrl.getBizProdCode());
            mchntAuthCtrlInfoBeanList.add(mchntAuthCtrlInfoBean);
        }
        rglog.debug("商户权限配置信息:{}", JSON.toJSONString(mchntAuthCtrlInfoBeanList));
        mchntTxnAuthQueryResponse.setMchntAuthCtrlInfoList(mchntAuthCtrlInfoBeanList);
        mchntTxnAuthQueryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        mchntTxnAuthQueryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntTxnAuthQueryResponse);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntTxnAuthQueryResponse mchntTxnAuthQueryResponse = (MchntTxnAuthQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntTxnAuthQueryResponse);
    }
}
